package com.ruoyi.project.business.nativedevice.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.controlcenter.iot.constant.CommonConstant;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.nativedevice.mapper.NativeDeviceMapper;
import com.ruoyi.project.business.nativedevice.entity.NativeDevice;
import com.ruoyi.project.business.nativedevice.request.ListNativeDeviceRequest;
import com.ruoyi.project.business.nativedevice.request.AddNativeDeviceRequest;
import com.ruoyi.project.business.nativedevice.response.NativeDeviceResponse;
import com.ruoyi.project.business.nativedevice.service.INativeDeviceService;

/**
 * 设备入网记录Service业务层处理
 * 
 * @author ${author}
 * @date 2024-07-26
 */
@Service
public class NativeDeviceServiceImpl implements INativeDeviceService {
    @Autowired
    private NativeDeviceMapper nativeDeviceMapper;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 查询设备入网记录
     * 
     * @return 设备入网记录
     */
    @Override
    public NativeDevice selectNativeDeviceById(Long id) {
        return nativeDeviceMapper.selectNativeDeviceById(id);
    }

    /**
     * 查询设备入网记录列表
     * 
     * @return 设备入网记录
     */
    @Override
    public List<NativeDeviceResponse> selectNativeDeviceList(ListNativeDeviceRequest request, Long tenantId) {
        // TODO request to  param
        NativeDevice param = BeanUtils.copy(request, NativeDevice.class);
//        param.setTenantId(tenantId);
        List<NativeDevice> entitis = nativeDeviceMapper.selectNativeDeviceList(param);
        List<NativeDeviceResponse> resList = BeanUtils.copyList(entitis, NativeDeviceResponse.class);
        return resList;
    }

    /**
     * 新增设备入网记录
     * 
     * @return 结果
     */
    @Override
    public int insertNativeDevice(AddNativeDeviceRequest request, Long operatorId, Long tenantId) {
        NativeDevice entity = BeanUtils.copy(request, NativeDevice.class);
        entity.setCreateTime(DateUtils.getNowDate());
        return nativeDeviceMapper.insertNativeDevice(entity);
    }

    /**
     * 修改设备入网记录
     * 
     * @return 结果
     */
    @Override
    public int updateNativeDevice(NativeDevice entity, Long operatorId, Long tenantId) {
        return nativeDeviceMapper.updateNativeDevice(entity);
    }

    /**
     * 批量删除设备入网记录
     * 
     * @return 结果
     */
    @Override
    public int deleteNativeDeviceByIds(Long[] ids, Long operatorId, Long tenantId) {
        return nativeDeviceMapper.deleteNativeDeviceByIds(ids);
    }

    /**
     * 删除设备入网记录信息
     * 
     * @return 结果
     */
    @Override
    public int deleteNativeDeviceById(Long id, Long operatorId, Long tenantId) {
        return nativeDeviceMapper.deleteNativeDeviceById(id);
    }


    private NativeDevice selectOneByMac(String ip, String mac) {
        NativeDevice param = new NativeDevice();
        param.setInnerNetIp(ip);
        param.setMac(mac);
        List<NativeDevice> entitis = nativeDeviceMapper.selectNativeDeviceList(param);
        if(CollectionUtils.isNotEmpty(entitis)) {
            return entitis.get(0);
        }
        return null;
    }



    @Override
    public void saveOrUpdateDevice(String ip, String mac, String deviceCode) {
        threadPoolTaskExecutor.execute(() -> {
            NativeDevice existed = this.selectOneByMac(ip, mac);
            if(existed != null && StringUtils.equals(existed.getInnerNetIp(), ip)) {
                existed.setInnerNetIp(ip);
                existed.setLastUpdateTime(new Date());
                nativeDeviceMapper.updateNativeDevice(existed);
            } else {
                NativeDevice entity = new NativeDevice();
                entity.setCreatorId(CommonConstant.SYSTEM_USER_ID);
                entity.setCreateTime(new Date());
                entity.setIsDeleted(Boolean.FALSE);
                entity.setDeviceCode(deviceCode);
                entity.setInnerNetIp(ip);
                entity.setMac(mac);
                nativeDeviceMapper.insertNativeDevice(entity);
            }
        });
//        ThreadPool.execute(() -> {
//
//            LambdaQueryWrapper<NativeDevice> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(NativeDevice::getMac, mac);
//            queryWrapper.eq(NativeDevice::getIsDeleted,  Boolean.FALSE);
//
//            NativeDevice existed = this.getOne(queryWrapper);
//            if(existed != null && !StringUtils.equals(existed.getInnerNetIp(), ip)) {
//                existed.setInnerNetIp(ip);
//                existed.setLastUpdateTime(new Date());
//                this.updateById(existed);
//            } else {
//                NativeDevice entity = new NativeDevice();
//                entity.setCreatorId(CommonConstant.SYSTEM_USER_ID);
//                entity.setCreateTime(new Date());
//                entity.setIsDeleted(Boolean.FALSE);
//                entity.setDeviceCode(deviceCode);
//                entity.setInnerNetIp(ip);
//                entity.setMac(mac);
//                this.save(entity);
//            }
//        });

    }
}
